package plantshop.view;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import plantshop.view.listeners.MenuMouseListener;


public class MainMenu extends JPanel {

	private static final long serialVersionUID = 1645657317518020036L;
	final static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	final static int frameWidth = screenSize.width;
   	final static int frameHeight = screenSize.height;
	final static double xscale = frameWidth/1600.0;
	final static double yscale = frameHeight/900.0;
	
	private transient Map<String, BufferedImage> images;
	private transient Map<String, BufferedImage[]> animations;
	
	public static final String MENU_TEXT = "Papyrus";
	public static final String BUTTON_TEXT = "Arial";
	public static final int[] SINGLE_POSITION = new int[]{(int)(465*xscale),(int)(340*yscale)};
	public static final int[] MULTI_POSITION = new int[]{(int)(465*xscale),(int)(487*yscale)};
	public static final int[] HELP_POSITION = new int[]{(int)(465*xscale),(int)(630*yscale)};
	public static final int BUTTON_HEIGHT = (int)(100*yscale);
	public static final int BUTTON_WIDTH = (int)(400*xscale);
	public static final int BUTTON_TEXT_SIZE = 36;
	public static final int TITLE_TEXT_SIZE = 120;
	
	public MainMenu(JFrame frame) throws IOException{
		images = new HashMap<String, BufferedImage>();
		animations = new HashMap<String, BufferedImage[]>();
		
		try {
			loadImages();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.addMouseListener(new MenuMouseListener(this, images, frame, animations));
	}
	public MainMenu(Map<String, BufferedImage> images, JFrame frame, Map<String, BufferedImage[]> animations) throws IOException{
		this.images = images;
		this.animations = animations;
		this.addMouseListener(new MenuMouseListener(this, images, frame, animations));
	}

	private BufferedImage scaleImage(BufferedImage img, int width, int height){
		BufferedImage before = img;
		double w = before.getWidth();
		double h = before.getHeight();
		BufferedImage after = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
		AffineTransform at = new AffineTransform();
		at.scale(width/w, height/h);
		AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
		return scaleOp.filter(before, after);
	}
	
	private void loadImages() throws IOException{
		
		images.put("Store", scaleImage(ImageIO.read(new File("images/PlantShop.png")), frameWidth, frameHeight));
		
		images.put("Display", scaleImage(ImageIO.read(new File("images/display.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Plant", ImageIO.read(new File("images/plant.png")));
		images.put("Butterflies", scaleImage(ImageIO.read(new File("images/butterfly.png")),(int)(35*xscale), (int)(35*yscale)));
		images.put("Dense Roots", scaleImage(ImageIO.read(new File("images/raindrop.png")),(int)(35*xscale), (int)(35*yscale)));
		images.put("Provide Shade", scaleImage(ImageIO.read(new File("images/shade_cloud.png")),(int)(35*xscale), (int)(35*yscale)));
		images.put("Nourish Wildlife", scaleImage(ImageIO.read(new File("images/nourishwildlife.png")),(int)(35*xscale), (int)(35*yscale)));
		images.put("Engage People", scaleImage(ImageIO.read(new File("images/human_interaction.png")),(int)(35*xscale), (int)(35*yscale)));
		
		images.put("Customer 1 Happy", scaleImage(ImageIO.read(new File("images/shopper_1_happy.png")), (int)(125*xscale), (int)(150*yscale)));
		images.put("Customer 1 Neutral", scaleImage(ImageIO.read(new File("images/shopper_1_neutral.png")), (int)(125*xscale), (int)(150*yscale)));
		images.put("Customer 1 Sad", scaleImage(ImageIO.read(new File("images/shopper_1_sad.png")), (int)(125*xscale), (int)(150*yscale)));
		images.put("Customer 2 Happy", scaleImage(ImageIO.read(new File("images/shopper_2_happy.png")), (int)(125*xscale), (int)(175*yscale)));
		images.put("Customer 2 Neutral", scaleImage(ImageIO.read(new File("images/shopper_2_neutral.png")), (int)(125*xscale), (int)(175*yscale)));
		images.put("Customer 2 Sad", scaleImage(ImageIO.read(new File("images/shopper_2_sad.png")), (int)(125*xscale), (int)(175*yscale)));
		images.put("Customer 3 Happy", scaleImage(ImageIO.read(new File("images/shopper_3_happy.png")), (int)(125*xscale), (int)(150*yscale)));
		images.put("Customer 3 Neutral", scaleImage(ImageIO.read(new File("images/shopper_3_neutral.png")), (int)(125*xscale), (int)(150*yscale)));
		images.put("Customer 3 Sad", scaleImage(ImageIO.read(new File("images/shopper_3_sad.png")), (int)(125*xscale), (int)(150*yscale)));
		
		//images.put("Male Player", scaleImage(ImageIO.read(new File("images/.png")),(int)(350*xscale),(int)(300*yscale)));
		//images.put("Female Player", scaleImage(ImageIO.read(new File("images/customer.png")),(int)(350*xscale),(int)(300*yscale)));
		
		images.put("Player Menu", scaleImage(ImageIO.read(new File("images/PlayerScreen.png")),(int)(frameWidth),(int)(frameHeight)));
		images.put("IPBox", scaleImage(ImageIO.read(new File("images/IPBox.png")),(int)(frameWidth),(int)(frameHeight)));
		images.put("Seeds", scaleImage(ImageIO.read(new File("images/seeds.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Main Menu",scaleImage(ImageIO.read(new File("images/TitleScreen.png")), (int)(frameWidth), (int)(frameHeight)));
		images.put("Title", scaleImage(ImageIO.read(new File("images/Title.png")), (int)(1500*xscale), (int)(900*yscale)));
		images.put("Menu",scaleImage(ImageIO.read(new File("images/menu.png")), (int)(800*xscale), (int)(600*yscale)));
		images.put("Instructions Menu",scaleImage(ImageIO.read(new File("images/menu.png")), (int)(1600*xscale), (int)(200*yscale)));
		images.put("Menu Aster", scaleImage(ImageIO.read(new File("images/AsterSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu Burning Bush", scaleImage(ImageIO.read(new File("images/Burning BushSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu Common Milkweed", scaleImage(ImageIO.read(new File("images/Common MilkweedSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu Eastern Red Cedar", scaleImage(ImageIO.read(new File("images/Eastern Red CedarSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu English Ivy", scaleImage(ImageIO.read(new File("images/English IvySeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu Japanese Barberry", scaleImage(ImageIO.read(new File("images/Japanese BarberrySeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu Norway Maple", scaleImage(ImageIO.read(new File("images/Norway_mapleSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu Sweet Pepperbush", scaleImage(ImageIO.read(new File("images/Sweet PepperbushSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Menu White Oak", scaleImage(ImageIO.read(new File("images/White OakSeedMenu.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Aster", scaleImage(ImageIO.read(new File("images/Aster.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Burning Bush", scaleImage(ImageIO.read(new File("images/Burning Bush.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Common Milkweed", scaleImage(ImageIO.read(new File("images/Common Milkweed.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Eastern Red Cedar", scaleImage(ImageIO.read(new File("images/Eastern Red Cedar.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("English Ivy", scaleImage(ImageIO.read(new File("images/English Ivy.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Japanese Barberry", scaleImage(ImageIO.read(new File("images/Japanese Barberry.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Norway Maple", scaleImage(ImageIO.read(new File("images/Norway_maple.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("Sweet Pepperbush", scaleImage(ImageIO.read(new File("images/Sweet Pepperbush.png")), (int)(100*xscale), (int)(100*yscale)));
		images.put("White Oak", scaleImage(ImageIO.read(new File("images/White Oak.png")), (int)(100*xscale), (int)(100*yscale)));
		
		
		images.put("butterfly", scaleImage(ImageIO.read(new File("images/butterfly.png")), (int)(35*xscale), (int)(35*yscale)));
		images.put("animal", scaleImage(ImageIO.read(new File("images/nourishwildlife.png")), (int)(50*xscale), (int)(50*yscale)));
		images.put("Money Sign", scaleImage(ImageIO.read(new File("images/moneysign.png")),(int)(35*xscale), (int)(35*yscale)));
		
		images.put("Tutorial0", scaleImage(ImageIO.read(new File("images/TutorialPlanting.png")), frameWidth,frameHeight));
		images.put("Tutorial1", scaleImage(ImageIO.read(new File("images/TutorialPickSeeds.png")), frameWidth,frameHeight));

		images.put("Skip", scaleImage(ImageIO.read(new File("images/Skip.png")), (int) (200*xscale), (int) (50*yscale)));
		images.put("Save", scaleImage(ImageIO.read(new File("images/Save.png")), (int) (200*xscale), (int) (50*yscale)));
		
		images.put("Rating Bar", scaleImage(ImageIO.read(new File("images/ratingbar.png")), (int)(175*xscale), (int)(55*yscale)));
		
		images.put("10", scaleImage(ImageIO.read(new File("images/Countdown10.png")), frameWidth,frameHeight));
		images.put("9", scaleImage(ImageIO.read(new File("images/Countdown9.png")), frameWidth,frameHeight));
		images.put("8", scaleImage(ImageIO.read(new File("images/Countdown8.png")), frameWidth,frameHeight));
		images.put("7", scaleImage(ImageIO.read(new File("images/Countdown7.png")), frameWidth,frameHeight));
		images.put("6", scaleImage(ImageIO.read(new File("images/Countdown6.png")), frameWidth,frameHeight));
		images.put("5", scaleImage(ImageIO.read(new File("images/Countdown5.png")), frameWidth,frameHeight));
		images.put("4", scaleImage(ImageIO.read(new File("images/Countdown4.png")), frameWidth,frameHeight));
		images.put("3", scaleImage(ImageIO.read(new File("images/Countdown3.png")), frameWidth,frameHeight));
		images.put("2", scaleImage(ImageIO.read(new File("images/Countdown2.png")), frameWidth,frameHeight));
		images.put("1", scaleImage(ImageIO.read(new File("images/Countdown1.png")), frameWidth,frameHeight));
		
		
		//Animations
		BufferedImage img = scaleImage(ImageIO.read(new File("images/invasivetakeover.png")), (int)(1000*xscale), (int)(150*yscale));
		BufferedImage[] pics = new BufferedImage[5];
		for(int i = 0; i < 5; i++)
			pics[i] = img.getSubimage((int)(200*xscale)*i, 0, (int)(200*xscale), (int)(150*yscale));
		animations.put("Invasive Takeover", pics);
	}
	
	@Override
	public void paint(Graphics g) {
		//Draw Store
		g.drawImage(images.get("Store"), 0, 0, this);
				
		//Draw Menu
		g.drawImage(images.get("Main Menu"), 0, 0, this);
	}
	
	public int[] getStartButtonCoords(){
		return new int[]{SINGLE_POSITION[0], SINGLE_POSITION[1], BUTTON_WIDTH,BUTTON_HEIGHT,
						 MULTI_POSITION[0],MULTI_POSITION[1],BUTTON_WIDTH,BUTTON_HEIGHT,
						 HELP_POSITION[0],HELP_POSITION[1],BUTTON_WIDTH,BUTTON_HEIGHT};
	}
}
